﻿Imports System.Windows.Forms
Imports System.Math




Public Class frmFindRing


    Dim r As CCloseRing
    Dim JGZ_PH_FILE1 As String
    Dim HXX_FILE1 As String
    Dim DDJWD_FILE1 As String
    Dim HBHC_FILE1 As String

    '参数 lineArray 边定义数据组（，1 to 10）,1 编号，2 开始点，3 结束点
    '参数 pointArray 点坐标定义数组（，1 to 10） 1 编号、2 经度、 3 纬度、 4高程 
    Dim rings(,), node(,), line(,) As String
    Dim classRings() As IClassTypeRing

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        r = New CCloseRing
        '参数 lineArray 边定义数据组（，1 to 10）,1 编号，2 开始点，3 结束点
        '参数 pointArray 点坐标定义数组（，1 to 10） 1 编号、2 经度、 3 纬度、 4高程 


        'ReDim node(5, 10), line(8, 10)
        'node(0, 1) = "D"
        'node(0, 2) = 0
        'node(0, 3) = 0
        'node(0, 4) = 0

        'node(1, 1) = "A"
        'node(1, 2) = 1
        'node(1, 3) = 1
        'node(1, 4) = 0

        'node(2, 1) = "B"
        'node(2, 2) = 2
        'node(2, 3) = 2
        'node(2, 4) = 0

        'node(3, 1) = "C"
        'node(3, 2) = 2
        'node(3, 3) = 1
        'node(3, 4) = 0

        'node(4, 1) = "E"
        'node(4, 2) = 1
        'node(4, 3) = 2
        'node(4, 4) = 0

        'node(5, 1) = "F"
        'node(5, 2) = 1
        'node(5, 3) = 2
        'node(5, 4) = 0


        'line(0, 1) = 4
        'line(0, 2) = "D"
        'line(0, 3) = "B"

        'line(1, 1) = 1
        'line(1, 2) = "A"
        'line(1, 3) = "B"

        'line(2, 1) = 2
        'line(2, 2) = "C"
        'line(2, 3) = "B"

        'line(3, 1) = 3
        'line(3, 2) = "A"
        'line(3, 3) = "C"


        'line(5, 1) = 5
        'line(5, 2) = "E"
        'line(5, 3) = "C"

        'line(6, 1) = 6
        'line(6, 2) = "A"
        'line(6, 3) = "E"

        'line(7, 1) = 7
        'line(7, 2) = "A"
        'line(7, 3) = "F"

        'line(8, 1) = 8
        'line(8, 2) = "B"
        'line(8, 3) = "F"

        '  On Error Resume Next


        Dim fread As ClassFileReader = New ClassFileReader

        'Timer1.Enabled = True

        'fread.readLineData("D:\JHF\ICloseCircle\ICloseCircle\bin\Debug\加改正后拼环中数-1.txt", line)
        'fread.readPointData("D:\JHF\ICloseCircle\ICloseCircle\bin\Debug\经纬度统计-3.txt", node)

        'fread.readLineData("test.txt", line)
        'fread.readPointData("testPoint.txt", node)


        'ReDim rings(0, 10)
        'r.findCloseRings(rings, line, node)
        'Dim kml As ClassKMLOut = New ClassKMLOut

        'kml.KML22OutLineString("linstr.KML", r.getResultRing)


    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Me.Close()
    End Sub

    Private Sub cmdReadLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReadLine.Click
        '边数据文件
        If Me.NodeDataGridView.RowCount <= 3 Then
            If MsgBox("节点坐标数据不存在，是否继续？", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "提示") <> MsgBoxResult.Yes Then
                Exit Sub
            End If
        End If



        Dim fread As ClassFileReader = New ClassFileReader
        openDialog.Reset()
        openDialog.Title = "测边数据文件"
        openDialog.ShowDialog()
        If openDialog.FileName = "" Then
            MsgBox("请正确选择边数据文件", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "提示：")
            Exit Sub
        End If

        JGZ_PH_FILE1 = openDialog.FileName '设置加改正拼环数据文件

        Dim tLines() As TypeLine
        Try
            Dim filename As String = openDialog.FileName
            fread.readLineData(filename, line)
            r.builderLines(line)
            tLines = r.getLines
            For Each tmpLine As TypeLine In tLines
                If Not tmpLine Is Nothing Then
                    Me.TypeLineBindingSource.Add(tmpLine)
                End If
            Next
            Me.LineToolStripStatusLabel1.Text = "共" & r.getLines.Length & "边"

        Catch ex As Exception
        End Try


        Me.LineDataGridView.Refresh()
        Me.TabControl1.SelectTab(tabLine)
    End Sub

    Private Sub cmdReadPoint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReadPoint.Click
        '导入坐标点文件
        Dim fread As ClassFileReader = New ClassFileReader
        openDialog.Reset()
        openDialog.Title = "点数据文件"
        openDialog.ShowDialog()
        If openDialog.FileName = "" Then
            MsgBox("请正确选择数据点文件", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "提示：")
            Exit Sub
        End If
        Dim filename As String = openDialog.FileName

        DDJWD_FILE1 = openDialog.FileName '设置经纬度文件点

        Try
            fread.readPointData(filename, node)
            r.builderNodes(node)
            Dim tNodes() As TypeNode
            tNodes = r.getNodes
            For Each tmpNode As TypeNode In tNodes
                If Not tmpNode Is Nothing Then
                    Me.TypeNodeBindingSource.Add(tmpNode)
                End If
            Next
            Me.PointToolStripStatusLabel1.Text = "共" & r.getNodes.Length & "点"

        Catch ex As Exception

        End Try
 

        Me.NodeDataGridView.Refresh()
        Me.TabControl1.SelectTab(tabPoint)

    End Sub
  
    Private Function isEmptyDate() As Boolean
        isEmptyDate = True
        If r.lines Is Nothing Then
            Exit Function
        End If

        '有一条非空边，认为数据合格
        For Each tmpLine As TypeLine In r.lines
            If Not tmpLine Is Nothing Then
                isEmptyDate = False
                Exit Function
            End If
        Next


    End Function

    Private Sub cmdCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCalc.Click
        '  On Error Resume Next

        If isEmptyDate Then
            '如果没有边数据，提示导入边数据
            MsgBox("请导入边数据", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "提示")
            Exit Sub

        End If

        'If UBound(node) < 1 Then
        '    '如果没有边数据，提示导入边数据
        '    MsgBox("请导入点坐标数据", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "提示")
        '    Exit Sub
        'End If
        Me.TabControl1.SelectTab(tabRing)

        ReDim rings(0, 10)
        Dim ST As String = Now
        Dim tmpr As CCloseRing = New CCloseRing
        tmpr.nodes = r.nodes.Clone
        tmpr.lines = r.lines.Clone

        '开始进行计时，显示滚动条滚动
        Timer1.Interval = 1000 '设置时间间隔为1秒 
        Timer1.Start()
        ' Dim t1 As System.Threading.Timer = New System.Threading.Timer(AddressOf Time1_callback)


        Me.ProgressBar1.Maximum = 100 ' Math.Pow(2, Math.Log(tmpr.nodes.Length))
        Me.ProgressBar1.Value = 0

        tmpr.searchedComplete = False

        Application.DoEvents()
        tmpr.findCloseRings()

        While tmpr.searchedComplete = False
            Threading.Thread.Sleep(50)
        End While



        r.resultRings = tmpr.resultRings.Clone

        classRings = tmpr.getResultRing.Clone
        For Each tmpRing As IClassTypeRing In classRings
            If Not tmpRing Is Nothing Then
                Me.IClassTypeRingBindingSource.Add(tmpRing)
            End If
        Next
        Me.RingDataGridView.Refresh()


        Timer1.Stop()
        Me.ProgressBar1.Value = 100

        Me.TabControl1.SelectTab(tabRing)
        MsgBox(ST & "_" & Now() & "计算完毕" & Trim(r.tmpRingsCount), MsgBoxStyle.OkOnly, "提示")

    End Sub

    Private Sub cmdExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExport.Click
        saveDialog.Filter = "Google KML 2.2(*.kml)|*.kml|文本数据文件(*.txt)|*.txt"

        saveDialog.ShowDialog()
        Dim filename = saveDialog.FileName

        HXX_FILE1 = saveDialog.FileName '设置环信息文件

        If filename = "" Then
            MsgBox("必须指定文件！", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical)
            Exit Sub
        End If
        Select Case saveDialog.FilterIndex
            Case 1
                Dim kml As ClassKMLOut = New ClassKMLOut
                kml.KML22OutLineString(filename, r.getResultRing)
                kml = Nothing
            Case 2
                '文本输出方式
                Dim txtOut As ClassTxtDataOut = New ClassTxtDataOut
                txtOut.TextFileOut(filename, r.getResultRing)
                txtOut = Nothing
        End Select

        Dim TEMM2 As String
        Dim AA As String = "C:\AAA.TXT"
        If saveDialog.FilterIndex = 2 Then
            '环信息文件加表头
            Dim I, J, NUM As Integer
            Dim HS(1000) As String
            ' Dim TQSJ_1 As String

            Dim DDHXX_FILE = My.Computer.FileSystem.OpenTextFileReader(HXX_FILE1, System.Text.Encoding.GetEncoding("gb2312"))
            J = 0 : I = 0
            With DDHXX_FILE
                Do Until DDHXX_FILE.EndOfStream '开始循环读取"加改正后拼环中数-1.txt"
                    HS(J) = DDHXX_FILE.ReadLine()  '单行读取,不能是数组,要加数组值
                    J += 1
                Loop
            End With
            NUM = J - 1 '设定加改正数的数据个数
            DDHXX_FILE.Close()
            Dim HCD(500, 8) As String

            '判断环的最大纬度及最小经度，进行排序，获取环的排序
            HPX(HS, NUM, DDJWD_FILE1, HCD)



            '写回文件中
            If My.Computer.FileSystem.FileExists(HXX_FILE1) Then
                '删除已有文件MsgBox("File found.") '如果文件存在则删除文件,为后续数据追加做准备
                My.Computer.FileSystem.DeleteFile(HXX_FILE1)
                '  Kill(dd_jwd_file) '删除已有文件JWD_FILE = checkpath & "经纬度原始文件.txt"
            Else
                ' MsgBox("File not found.")
            End If


            My.Computer.FileSystem.WriteAllText(HXX_FILE1, NUM + 1 & vbCrLf, True) '写入行数

            For J = 0 To NUM
                TEMM2 = ""
                My.Computer.FileSystem.WriteAllText(HXX_FILE1, HS(J) & vbCrLf, True)
                For I = 0 To 6
                    TEMM2 &= HCD(J, I) & ","
                Next I
                My.Computer.FileSystem.WriteAllText(AA, TEMM2 & vbCrLf, True)
            Next J
            DDHXX_FILE.Close()
        End If


        MsgBox("文件输出完成！")
    End Sub
    Sub HPX(ByRef HS() As String, ByVal NUM As Integer, ByVal DDJWD_FILE1 As String, ByVal HCD(,) As String)
        '环排序及获取环中点经纬度

        Dim I, J, k, nuM_d As Integer
        '  Dim HS(1000) As String
        ' Dim 
        Dim TQSJ_1 As String
        Dim strarray(), strarray1(), strarray2(), strjwd(3000, 5), HSC(500, 3), PXSZ(500, 8) As String

        Dim DDjws_FILE = My.Computer.FileSystem.OpenTextFileReader(DDJWD_FILE1, System.Text.Encoding.GetEncoding("gb2312"))
        J = 0 : I = 0
        With DDjws_FILE
            Do Until DDjws_FILE.EndOfStream '开始循环读取"加改正后拼环中数-1.txt"
                TQSJ_1 = DDjws_FILE.ReadLine()  '单行读取,不能是数组,要加数组值
                strarray = TQSJ_1.Split(",")
                For I = 0 To strarray.Length - 1
                    strjwd(J, I) = strarray(I)
                Next I
                J += 1
            Loop
        End With
        NUM_d = J - 1 '设定加改正数的数据个数
        DDjws_FILE.Close()
        ' GB01,2006003,2,101.2757,36.4234
        Dim max_wd, max_jd, min_wd, min_jd, JD, WD As String

        J = 0 : I = 0 : k = 0
        For J = 0 To NUM
            max_wd = "" : max_jd = "" : min_wd = "" : min_jd = ""

            '拆分环数据
            strarray1 = HS(J).Split(",")
            For I = 0 To strarray1.Length - 1
                HSC(J, I) = strarray1(I)
            Next I
            '拆分环信息数组
            strarray2 = HSC(J, 1).Split(">")
            For k = 0 To strarray2.Length - 1
                '获取经纬度数据
                JD = ""
                WD = ""
                For p = 0 To nuM_d
                    If strarray2(k) = strjwd(p, 0) Then
                        '如果点名与经纬度文件中的点名一致，获取经纬度后退出循环
                        JD = strjwd(p, 3)
                        WD = strjwd(p, 4)
                        '设置判断最大与最小点
                        If max_wd < WD Then
                            '替换最大纬度
                            max_wd = WD
                        End If
                        If max_jd < JD Then
                            '替换最大经度
                            max_jd = JD
                        End If
                        '如果为空，永远不会大于空
                        If k = 0 Then '将第一点经纬度赋予最小值
                            min_jd = JD
                            min_wd = WD
                        Else
                            If min_wd > WD Then
                                '替换最大纬度
                                min_wd = WD
                            End If
                            If min_jd > JD Then
                                '替换最大经度
                                min_jd = JD
                            End If
                        End If '第一点判断结束
                        Exit For
                    End If
                Next p
                '要有一个条件式，否者将退出循环K
                If JD = "" And WD = "" Then
                    MsgBox(strarray2(k) & "没有找到！")
                End If

            Next k '环点循环找坐标
            PXSZ(J, 0) = HSC(J, 0) '环号
            PXSZ(J, 1) = HSC(J, 1) '环路线
            PXSZ(J, 2) = HSC(J, 2) '环点数
            PXSZ(J, 3) = max_wd '最大纬度
            PXSZ(J, 4) = min_jd '最小经度
            PXSZ(J, 5) = min_wd '最小纬度
            PXSZ(J, 6) = max_jd '最大经度
        Next J


        '按最大纬度，最小精度排序
        Dim a(8) As String
        For J = 0 To NUM

            For I = 0 To NUM
                If PXSZ(J, 3) >= PXSZ(I, 3) Then '最大纬度前点小于后点时交换
                    For P = 0 To 6
                        a(P) = PXSZ(J, P)
                        PXSZ(J, P) = PXSZ(I, P)
                        PXSZ(I, P) = a(P)
                    Next P
                ElseIf (Abs(Val(PXSZ(J, 3)) - Val(PXSZ(I, 3))) < 0.01) And PXSZ(J, 4) >= PXSZ(I, 4) Then '最大纬度相等，最小经度前点大于后点时交换
                    For P = 0 To 6
                        a(P) = PXSZ(J, P)
                        PXSZ(J, P) = PXSZ(I, P)
                        PXSZ(I, P) = a(P)
                    Next P
                End If

            Next I

        Next J
        '赋给返回值
        For J = 0 To NUM
            HS(J) = J + 1 & "," & PXSZ(J, 1) & "," & PXSZ(J, 2)
            HCD(J, 0) = J + 1
            HCD(J, 1) = PXSZ(J, 1)
            HCD(J, 2) = PXSZ(J, 2)
            HCD(J, 3) = PXSZ(J, 3)
            HCD(J, 4) = PXSZ(J, 4)
            HCD(J, 5) = PXSZ(J, 5)
            HCD(J, 6) = PXSZ(J, 6)

        Next J





    End Sub
    Private Sub cmdConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConfig.Click
        'Timer1.Start()

        'Do While True
        '    System.Threading.Thread.Sleep(10)
        'Loop



        Dim config As frmConfig
        config = New frmConfig
        config.ShowDialog()
    End Sub

    Private Sub LineDataGridView_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NodeDataGridView.MouseClick, LineDataGridView.MouseClick, RingDataGridView.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Me.Popup_Menu.Show(sender, e.Location)
        End If
    End Sub


    Private Sub PopupMenu_Refresh_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PopupMenu_Refresh.Click
        If Me.TabControl1.SelectedTab.Name = "tabLine" Then
            LineDataRefresh()
        ElseIf Me.TabControl1.SelectedTab.Name = "tabRing" Then
            RingDataRefresh()
        ElseIf Me.TabControl1.SelectedTab.Name = "tabPoint" Then
            NodeDataRefresh()
        End If
    End Sub

    Private Sub NodeDataRefresh()
        Dim tNodes() As TypeNode
        tNodes = r.getNodes
        Me.TypeNodeBindingSource.Clear()
        If Not tNodes Is Nothing Then
            For Each tmpNode As TypeNode In tNodes
                If Not tmpNode Is Nothing Then
                    Me.TypeNodeBindingSource.Add(tmpNode)
                End If
            Next
        End If
        Me.NodeDataGridView.Refresh()
        Me.TabControl1.SelectTab(tabPoint)
    End Sub

    Private Sub LineDataRefresh()

        Me.TypeLineBindingSource.Clear()

        Dim tLines() As TypeLine
        tLines = r.getLines
        If Not tLines Is Nothing Then
            For Each tmpLine As TypeLine In tLines
                If Not tmpLine Is Nothing Then
                    Me.TypeLineBindingSource.Add(tmpLine)
                End If
            Next
        End If
        Me.LineDataGridView.Refresh()
        Me.TabControl1.SelectTab(tabLine)
    End Sub
    Private Sub RingDataRefresh()

        Me.IClassTypeRingBindingSource.Clear()

        Dim classRings() As IClassTypeRing
        classRings = r.getResultRing
        If Not classRings Is Nothing Then

            For Each tmpRing As IClassTypeRing In classRings
                If Not tmpRing Is Nothing Then
                    Me.IClassTypeRingBindingSource.Add(tmpRing)
                End If
            Next
        End If

        Me.RingDataGridView.Refresh()
        Me.TabControl1.SelectTab(tabRing)
    End Sub



    Private Sub Popup_Menu_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Popup_Menu_Clear.Click

        If Me.TabControl1.SelectedTab.Name = "tabLine" Then
            Array.Clear(r.lines, 0, r.lines.Length)
            Me.TypeLineBindingSource.Clear()
            Me.LineDataGridView.Refresh()
        ElseIf Me.TabControl1.SelectedTab.Name = "tabRing" Then
            Array.Clear(r.resultRings, 0, r.resultRings.Length)
            Me.IClassTypeRingBindingSource.Clear()
            Me.RingDataGridView.Refresh()
        ElseIf Me.TabControl1.SelectedTab.Name = "tabPoint" Then
            Array.Clear(r.nodes, 0, r.nodes.Length)
            Me.TypeNodeBindingSource.Clear()
            Me.NodeDataGridView.Refresh()
        End If

    End Sub



    Private Sub FlowLayoutPanel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles FlowLayoutPanel1.Paint

    End Sub

    Private Sub TypeNodeBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TypeNodeBindingSource.CurrentChanged

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '环闭合差计算
        '计算环限差及环闭合差
        'Me.ToolStripStatusLabel2.Text = " 环闭合差计算："
        'Me.ToolStripStatusLabel3.Text = " ......."
        'If Me.ToolStripStatusLabel1.Text = "路径" Or Me.ToolStripStatusLabel1.Text = "" Then '如果检查路径为空则打开工程
        '    OpenFileDialog1.Filter = "text files (*.prj)|*.prj"
        '    OpenFileDialog1.ShowDialog()
        '    If OpenFileDialog1.FileName = "" Then
        '        Exit Sub
        '    End If
        '    openProject(OpenFileDialog1.FileName)
        'End If
        'Dim checkpath As String = Me.ToolStripStatusLabel1.Text '设置检查文件目录为主界面ToolStripStatusLabel1.TEXT（文件目录中带\）

        Dim check_file As String = "C:\检查问题文件.txt"
        'Dim HXX_FILE1 As String = checkpath & "环信息文件.txt"
        'Dim JGZ_PH_FILE1 As String = checkpath & "加改正后拼环中数-1.txt"


        'Dim HBHC_FILE1 As String = checkpath & "环闭合差计算-1.txt"

        saveDialog.ShowDialog()
        Dim filenameH As String = saveDialog.FileName

        HBHC_FILE1 = saveDialog.FileName '设置环信息文件

        If filenameH = "" Or HXX_FILE1 = "" Or JGZ_PH_FILE1 = "" Then
            MsgBox("必须指定文件！", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical)
            Exit Sub
        End If

        If My.Computer.FileSystem.FileExists(HBHC_FILE1) Then
            '如果文件存在则删除文件,为后续数据追加做准备
            My.Computer.FileSystem.DeleteFile(HBHC_FILE1)
            '   Kill(HBHC_FILE) '删除已有文件HBHC_FILE = checkpath & "\环闭合差计算-1.txt"
        End If



        'If My.Computer.FileSystem.FileExists(JGZ_PH_FILE1) And My.Computer.FileSystem.FileExists(HXX_FILE1) Then
        '    '文件存在;判断计算的2个文件是否存在
        'Else
        '    '文件不存在
        '    MsgBox("《环信息文件.txt》或《加改正后拼环中数-1.txt》没有找到,请确认文件存在后再次计算.", vbOKOnly)
        '    '退出当前程序
        '    Exit Sub
        'End If

        Dim distanceM, distance1, distance2, distance3, HXC, BHC, HBFB As Double
        '打开拼环信息文件
        Dim DDHXX_FILE = My.Computer.FileSystem.OpenTextFileReader(HXX_FILE1, System.Text.Encoding.GetEncoding("gb2312"))
        Dim DDJGZPH_FILE = My.Computer.FileSystem.OpenTextFileReader(JGZ_PH_FILE1, System.Text.Encoding.GetEncoding("gb2312"))



        ' 以下程序
        Dim I, J, K, L, num, A, DS As Integer
        Dim strarray(), strarray1(), str1(10000), check1, HSC As String
        Dim PHsrt6(10000, 6), HXXsrt6(500, 5) As String '拼环的数据文件数组
        Dim tqsj_1, tqsj_2 As String

        Dim HX, LX, H5(500), TEMP2 As String
        '  Dim TEMP1, DDJ, DS, DS1, DS2, DS3, H6(5000) As Integer

        J = 0 : I = 0
        With DDJGZPH_FILE
            Do Until DDJGZPH_FILE.EndOfStream '开始循环读取"加改正后拼环中数-1.txt"
                tqsj_1 = DDJGZPH_FILE.ReadLine()  '单行读取,不能是数组,要加数组值
                '将一维数据变为2维数组
                strarray = tqsj_1.Split(",")
                For I = 0 To strarray.Length - 1
                    PHsrt6(J, I) = strarray(I)
                Next I
                J += 1

            Loop
        End With
        num = J - 1 '设定加改正数的数据个数
        DDJGZPH_FILE.Close()
        J = 0 : I = 0
        ' With DDHXX_FILE‘


        A = Val(DDHXX_FILE.ReadLine()) '读取第一行数据,总环数
        For L = 1 To A '循环读取与计算环数据

            '读取环信息数据行
            tqsj_2 = DDHXX_FILE.ReadLine()  '读取第二行数据
            strarray = tqsj_2.Split(",")
            HX = strarray(0) '环线号
            TEMP2 = strarray(1) '环线点名字串
            DS = strarray(2) '环线点数
            '将点名字串变为数组
            strarray1 = TEMP2.Split(">")

            For I = 0 To strarray1.Length - 1
                H5(I) = strarray1(I) '将点名串变为单数组点名
            Next I
            H5(DS) = H5(0)

            '计算环闭合差-配对
            distance1 = 0
            distance2 = 0
            distance3 = 0
            BHC = 0
            distanceM = 0
            LX = ""
            For J = 0 To DS - 1  '循环查找各测段点之间的高差距离等级,计算各等级测段距离
                For K = 0 To num  '循环提取测段数据
                    If (H5(J) = PHsrt6(K, 0) And H5(J + 1) = PHsrt6(K, 1)) Or (H5(J) = PHsrt6(K, 1) And H5(J + 1) = PHsrt6(K, 0)) Then
                        If H5(J) = PHsrt6(K, 1) And H5(J + 1) = PHsrt6(K, 0) Then '高差反向顺序判断
                            BHC += (-1) * PHsrt6(K, 2)
                        Else
                            BHC += PHsrt6(K, 2)
                        End If

                        If Val(PHsrt6(K, 5)) = 1 Then ' 判断等级
                            distance1 += PHsrt6(K, 3)
                        ElseIf Val(PHsrt6(K, 5)) = 2 Then
                            distance2 += PHsrt6(K, 3)
                        Else
                            If Val(PHsrt6(K, 5)) = 3 Then
                                distance3 += PHsrt6(K, 3)
                            Else
                                MsgBox("拼环等级不为1-2-3", vbOKOnly)
                            End If
                        End If ' 判断等级结束
                        distanceM += PHsrt6(K, 3)
                        LX &= H5(J) & "→"

                        Exit For  '配对成功后退出循环查找
                    Else
                        '没有配对成功
                        If K = num Then '判断到最后一个数据时
                            check1 = "拼环数据没有配对成功:" & HX & "：" & H5(J) & " 与 " & H5(J + 1) & ";《环信息文件.txt》和《加改正后拼环中数-1》.txt"
                            My.Computer.FileSystem.WriteAllText(check_file, check1 & vbCrLf, True)
                            MsgBox(check1, vbOKOnly)
                            ' Exit Sub
                        End If
                    End If '配对结束

                Next K '循环提取测段数据结束

            Next J '循环查找各测段点之间的高差距离等级,计算各等级测段距离结束
            LX = LX & H5(DS)
            '计算环闭合差
            If distance3 <> 0 Then '三等
                ' 计算闭合差及限差
                HXC = Sqrt(2 * 2 * distance1 + 16 * distance2 + 144 * distance3)
                HBFB = Abs(BHC * 1000) / HXC * 100
                '保存到文件()
                HXC = djsbj(HXC, 2)
                HBFB = djsbj(HBFB, 1)
                BHC = djsbj(BHC * 1000, 2)
                HSC = HX & ",3," & LX & "," & DS & "," & distance1 & "," & distance2 & "," & distance3 & "," & distanceM & "," & BHC & "," & "±" & HXC & "," & HBFB
                '写入\环闭合差计算-1.txt文件
                My.Computer.FileSystem.WriteAllText(HBHC_FILE1, HSC & vbCrLf, True)
            ElseIf distance2 <> 0 And distance3 = 0 Then '二等
                HXC = Sqrt(2 * 2 * distance1 + 16 * distance2)
                HBFB = Abs(BHC * 1000) / HXC * 100
                '保存到文件
                HXC = djsbj(HXC, 2)
                HBFB = djsbj(HBFB, 1)
                BHC = djsbj(BHC * 1000, 2)
                HSC = HX & ",2," & LX & "," & DS & "," & distance1 & "," & distance2 & "," & distance3 & "," & distanceM & "," & BHC & "," & "±" & HXC & "," & HBFB
                '写入\环闭合差计算-1.txt文件
                My.Computer.FileSystem.WriteAllText(HBHC_FILE1, HSC & vbCrLf, True)
            Else '一等
                HXC = Sqrt(2 * 2 * distance1)
                HBFB = Abs(BHC * 1000) / HXC * 100
                '保存到文件
                HXC = djsbj(HXC, 2)
                HBFB = djsbj(HBFB, 1)
                BHC = djsbj(BHC * 1000, 2)
                HSC = HX & ",1," & LX & "," & DS & "," & distance1 & "," & distance2 & "," & distance3 & "," & distanceM & "," & BHC & "," & "±" & HXC & "," & HBFB
                '写入\环闭合差计算-1.txt文件
                My.Computer.FileSystem.WriteAllText(HBHC_FILE1, HSC & vbCrLf, True)
            End If
        Next L

        FileClose() '关闭以上所有文件



        '输出至表格
        Dim YZGCFILE = My.Computer.FileSystem.OpenTextFileReader(HBHC_FILE1, System.Text.Encoding.GetEncoding("gb2312"))
        'Dim i, j, k As Integer
        Dim RNT, CNT As Integer
        Dim temm1 As String
        '  Dim strarray() As String
        Dim srt2(5000, 11) As String
        '  Dim TEMP2(5000) As Object
        '   读取已知高程文件
        J = 0
        With YZGCFILE
            Do Until YZGCFILE.EndOfStream '开始循环读取LR文件中的标准数据
                temm1 = YZGCFILE.ReadLine()  '单行读取,不能是数组,要加数组值
                '将数组拆分为2维数组,
                strarray = temm1.Split(",")
                CNT = strarray.Length  '实际列数
                For I = 0 To strarray.Length - 1
                    srt2(J, I) = strarray(I)
                Next I
                J = J + 1
            Loop
        End With
        RNT = J  '实际行数
        YZGCFILE.Close()
        '写入数据
        ' Create an unbound DataGridView by declaring a column count.
        DataGridView1.ColumnCount = CNT
        DataGridView1.RowCount = RNT + 1 '+1是为最后一行为空做准备的,否则不需要加
        DataGridView1.ColumnHeadersVisible = True
        DataGridView1.ReadOnly = True '设置单元格只读
        ' Set the column header style.
        Dim columnHeaderStyle As New DataGridViewCellStyle()

        'columnHeaderStyle.BackColor = Color.Beige
        'columnHeaderStyle.Font = New Font("Verdana", 9, FontStyle.Bold)
        DataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle
        DataGridView1.Columns(1).FillWeight = 5 '设置第二列的宽度位5%
        ' Set the column header names.设置表头文件
        ' 《往返测段数据精度-1.txt》其文件格式为：0001,2,AD13,AD12,-2.73714,2.72994,-2.7336,21.9,-7.2,±18.72,38.5
        '0-2位为：0环线号、1等级、2环路径；3-7位为：3环测段数、4一等距离、5二等距离、6三等距离、7环线距离
        '8-10位为：8环闭合差、9环限差、10百分比
        DataGridView1.Columns(0).Name = "环线号"
        DataGridView1.Columns(1).Name = "等级"
        DataGridView1.Columns(2).Name = "环路径"
        DataGridView1.Columns(3).Name = "环测段数"
        DataGridView1.Columns(4).Name = "一等距离"
        DataGridView1.Columns(5).Name = "二等距离"
        DataGridView1.Columns(6).Name = "三等距离"
        DataGridView1.Columns(7).Name = "环线距离"
        DataGridView1.Columns(8).Name = "环闭合差"
        DataGridView1.Columns(9).Name = "环限差"
        DataGridView1.Columns(10).Name = "百分比"

        '放置最后一行位空行,焦点位置;与保存数据格式一致,否则最后一行数据将丢失哦！
        'For i = 0 To CNT - 1
        '    srt2(RNT, i) = " "
        'Next i

        '  写入屏幕DATAGRIDE中
        I = 0 : K = 0
        For K = 0 To RNT
            '设置单元格颜色
            If srt2(K, 10) > 1 Then
                '超限用红色


            ElseIf srt2(K, 10) < 1 And srt2(K, 10) >= 3 / 4 Then
                '大于3/4用蓝色

            Else
                '其他用默认黑色

            End If

            For I = 0 To CNT - 1
                Me.DataGridView1.Rows(K).Cells(I).Value = srt2(K, I)
            Next I
        Next K

        ' MsgBox("导入测段汇总完成", vbOK)
        FileClose()


        ''显示检查问题窗口文字
        '' Dim check_file As String = Me.ToolStripStatusLabel1.Text & "检查问题文件.txt"
        'WENZS(check_file)

        'FileClose()
        'Me.ToolStripStatusLabel3.Text = "环闭合差计算完成。"

    End Sub

    Public Function djsbj(ByVal yuan_zhi As Double, ByVal weishu As Integer) As Double '单进双不进(yuan_zhi-原值,weishu-位数)
        '单进双不进
        Dim result_zhi As Double
        Dim result_zhi_tem As Double
        Dim Original_zhi As Double
        Dim tem_zhi1 As String
        Dim tem_zhi2 As String
        Dim fh As Integer
        If yuan_zhi > 0.00000001 Or yuan_zhi < -0.00000001 Then '判断不为0时才能进行转换，否则的0退出
            fh = Sign(yuan_zhi) '将负值变为正直,最后再乘以负值符号
            yuan_zhi = Abs(yuan_zhi)

            result_zhi_tem = Int(yuan_zhi * (10 ^ (weishu + 1)))
            Original_zhi = Int(yuan_zhi * (10 ^ (weishu))) / (10 ^ weishu)
            ' tem_zhi1 = Right(CStr(result_zhi_tem), 1) 'lao
            tem_zhi1 = CStr(result_zhi_tem).Substring(CStr(result_zhi_tem).Length - 1, 1)
            ' tem_zhi2 = Left(Right(CStr(result_zhi_tem), 2), 1) 'lao
            tem_zhi2 = CStr(result_zhi_tem).Substring(CStr(result_zhi_tem).Length - 2, 1)
            'If yuan_zhi - Original_zhi > 0 Then result_zhi = Original_zhi + (10 ^ -(weishu))
            If CInt(tem_zhi1) < 5 Then
                result_zhi = (Int(result_zhi_tem / 10)) / (10 ^ weishu)
            Else
                If CInt(tem_zhi1) = 5 Then
                    If CInt(tem_zhi2) Mod 2 = 0 Then
                        result_zhi = (Int(result_zhi_tem / 10)) / (10 ^ weishu)
                    Else
                        result_zhi = (Int(result_zhi_tem / 10) + 1) / (10 ^ weishu)
                    End If
                    If (Int((yuan_zhi - Original_zhi) * 10 ^ (weishu + 1) * 100000000) / 100000000) > 5 Then result_zhi = Original_zhi + (10 ^ -(weishu))
                Else
                    result_zhi = (Int(result_zhi_tem / 10) + 1) / (10 ^ weishu)
                End If
            End If
            djsbj = result_zhi * fh
        Else
            djsbj = 0
        End If
    End Function
    Public Function DEG(ByVal m As Double)
        '度分秒转化为度小数
        Dim D, MI, S As Double
        D = Int(m)
        MI = Int((m - D) * 100)
        S = ((m - D) * 100 - MI) * 100
        DEG = D + MI / 60 + S / 3600
    End Function

    Private Sub Timer1_Tick1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Me.ProgressBar1.Value > 95 Then
            Me.ProgressBar1.Value = 80
        End If

        Me.ProgressBar1.Value += 1

        Me.ProgressBar1.Refresh()
    End Sub

    Private Sub Time1_callback()
        Throw New NotImplementedException
        Me.ProgressBar1.Value += 1
        Me.ProgressBar1.Refresh()
    End Sub

    Private Sub btnHandComp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHandComp.Click
        Dim tool As ArcgisGraphTool = New ArcgisGraphTool()
        Dim a = 3, b = 4
        Dim r1 = r.getResultRingList.Item(a)
        Dim r2 = r.getResultRingList.Item(b)
        ArcgisGraphTool.isInterSectRingArcGis(r1, r2)
    End Sub
End Class
